package indi.zhifa.recipe.bailan.framework.auth.controller.api;

import indi.zhifa.recipe.bailan.framework.auth.entity.annotations.DataPermission;
import indi.zhifa.recipe.bailan.framework.auth.entity.annotations.RequireRole;
import indi.zhifa.recipe.bailan.framework.auth.entity.po.RelUserRoleEntity;
import indi.zhifa.recipe.bailan.framework.auth.service.IUserAuthService;
import indi.zhifa.recipe.bailan.framework.common.entity.annotations.ZfRestController;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "UserDepartmentApi-用户部门管理")
@RequestMapping("/api/userDep")
@Slf4j
@ZfRestController
@RequiredArgsConstructor
public class UserAuthApi {

    final IUserAuthService mUserAuthService;

    @RequireRole(roles = {"accountMgr"})
    @DataPermission(domain = "zf-account")
    @Operation(summary = "改变用户部门")
    @PutMapping(value = "/{userId}/domain/{domainId}")
    public String changeDepartment(
            @Parameter(description = "用户Id") @PathVariable(name = "userId") Long pUserId,
            @Parameter(description = "领域Id") @PathVariable(name = "domainId") Long pDomainId,
            @Parameter(description = "部门Id") @RequestParam(name = "depId") Long pDepartmentId){
        mUserAuthService.changeDepartment(pUserId,pDomainId,pDepartmentId);
        return "更改成功";
    }

    @RequireRole(roles = {"accountMgr"})
    @DataPermission(domain = "zf-account")
    @Operation(summary = "改变用户角色")
    @PutMapping(value = "/{userId}/roles/bycode")
    public List<RelUserRoleEntity> assignRolesWithCode(
            @Parameter(description = "用户Id") @PathVariable(name = "userId") Long pUserId,
            @Parameter(description = "角色codes") @RequestBody List<String> pRoleCodes){
        List<RelUserRoleEntity> relUserRoleEntityList =  mUserAuthService.assignRolesWithCode(pUserId,pRoleCodes);
        return relUserRoleEntityList;
    }

    @RequireRole(roles = {"accountMgr"})
    @DataPermission(domain = "zf-account")
    @Operation(summary = "改变用户角色")
    @PutMapping(value = "/{userId}/roles/byId")
    public List<RelUserRoleEntity> assignRolesWithId(
            @Parameter(description = "用户Id") @PathVariable(name = "userId") Long pUserId,
            @Parameter(description = "角色Ids") @RequestBody List<Long> pRoleIds){
        List<RelUserRoleEntity> relUserRoleEntityList =  mUserAuthService.assignRolesWithId(pUserId,pRoleIds);
        return relUserRoleEntityList;
    }

}
